/**
 * Title :	Property view
 * Description: Property editor
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package bsys.propertyview;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableCellEditor;

/**
 * File editor
 * 
 * @author Bernard Bou
 */
class FileEditor extends AbstractCellEditor implements TableCellEditor
{
	private static final long serialVersionUID = 1L;

	/**
	 * File chooser
	 */
	private JFileChooser theFileChooser;

	/**
	 * Button component
	 */
	private JButton theButton;

	/**
	 * Current directory
	 */
	private File theCurrentDirectory;

	/**
	 * Constructor
	 */
	public FileEditor()
	{
		theButton = new JButton();
		theButton.setBorderPainted(false);
		theButton.setHorizontalAlignment(SwingConstants.LEFT);
		theButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theFileChooser.setCurrentDirectory(theCurrentDirectory);
				theFileChooser.showOpenDialog(null);

				// dialog returns control
				fireEditingStopped();
			}
		});

		// file chooser
		JButton thisDefaultFileButton = new JButton(Messages.getString("FileEditor.0")); //$NON-NLS-1$
		thisDefaultFileButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theFileChooser.setSelectedFile(null);
				Container thisContainer = theFileChooser.getParent();
				while (thisContainer.getParent() != null)
					thisContainer = thisContainer.getParent();
				thisContainer.setVisible(false);
			}
		});
		theFileChooser = new JFileChooser();
		theFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		theFileChooser.setFileFilter(new FileFilter()
		{
			public boolean accept(File thisFile)
			{
				return thisFile.getName().toLowerCase().endsWith(".gif") || thisFile.getName().toLowerCase().endsWith(".jpg") || thisFile.getName().toLowerCase().endsWith(".png") || thisFile.isDirectory(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}

			public String getDescription()
			{
				return Messages.getString("FileEditor.1"); //$NON-NLS-1$
			}
		});
		theFileChooser.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (e.getActionCommand().equalsIgnoreCase(JFileChooser.APPROVE_SELECTION))
					fireEditingStopped();
				else
					fireEditingCanceled();
			}
		});
		theFileChooser.setAccessory(thisDefaultFileButton);
	}

	/**
	 * Set current directory
	 * 
	 * @param thisCurrentDirectory
	 *        current directory
	 */
	public void setDirectory(File thisCurrentDirectory)
	{
		theCurrentDirectory = thisCurrentDirectory;
	}

	// I N T E R F A C E
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
	 */
	public Component getTableCellEditorComponent(JTable thisTable, Object thisValue, boolean thisIsSelected, int thisRow, int thisColumn)
	{
		theButton.setText((String) thisValue);
		return theButton;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.CellEditor#getCellEditorValue()
	 */
	public Object getCellEditorValue()
	{
		File thisSelection = theFileChooser.getSelectedFile();
		return thisSelection == null ? null : thisSelection.getName();
	}
}
